{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font>Hadamard</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1 </h3>\n",
    "\n",
    "Remember that x-gate flips the value of a qubit.\n",
    "\n",
    "Design a quantum circuit with a single qubit.\n",
    "\n",
    "The qubit is initially set to $ \\ket{0} $.\n",
    "\n",
    "Set the value of qubit to $ \\ket{1} $ by using x-gate.\n",
    "\n",
    "Experiment 1: Apply one Hadamard gate, make measurement, and execute your program 10000 times.\n",
    "\n",
    "Experiment 2: Apply two Hadamard gates, make measurement, and execute your program 10000 times.\n",
    "\n",
    "Compare your results.\n",
    "\n",
    "The following two diagrams represent two different experiments."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table>\n",
    "    <tr>\n",
    "        <td><img src=\"../images/photon8.jpg\" width=\"80%\"></td>\n",
    "        <td><img src=\"../images/photon9.jpg\" width=\"70%\"></td>\n",
    "    </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>\n",
    "\n",
    "<h4>Experiment 1: x-gate, h-gate, and measurement </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "# define a quantum register with one qubit\n",
    "qreg3 =  QuantumRegister(1)\n",
    "\n",
    "# define a classical register with one bit\n",
    "# it stores the measurement result of the quantum part\n",
    "creg3 = ClassicalRegister(1)\n",
    "\n",
    "# define our quantum circuit\n",
    "mycircuit3 = QuantumCircuit(qreg3,creg3)\n",
    "\n",
    "# apply x-gate to the first qubit\n",
    "mycircuit3.x(qreg3[0])\n",
    "\n",
    "# apply h-gate (Hadamard: quantum coin-flipping) to the first qubit\n",
    "mycircuit3.h(qreg3[0])\n",
    "\n",
    "# measure the first qubit, and store the result in the first classical bit\n",
    "mycircuit3.measure(qreg3,creg3)\n",
    "\n",
    "print(\"Everything looks fine, let's continue ...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# draw the circuit\n",
    "drawer(mycircuit3)\n",
    "# reexecute me if you DO NOT see the circuit diagram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# execute the circuit and read the results\n",
    "job = execute(mycircuit3,Aer.get_backend('qasm_simulator'),shots=10000)\n",
    "\n",
    "counts3 = job.result().get_counts(mycircuit3)\n",
    "print(counts3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Experiment 2: x-gate, h-gate, h-gate, and measurement </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "# define a quantum register with one qubit\n",
    "qreg4 =  QuantumRegister(1)\n",
    "\n",
    "# define a classical register with one bit\n",
    "# it stores the measurement result of the quantum part\n",
    "creg4 = ClassicalRegister(1)\n",
    "\n",
    "# define our quantum circuit\n",
    "mycircuit4 = QuantumCircuit(qreg4,creg4)\n",
    "\n",
    "# apply x-gate to the first qubit\n",
    "mycircuit4.x(qreg4[0])\n",
    "\n",
    "# apply h-gate (Hadamard: quantum coin-flipping) to the first qubit twice\n",
    "mycircuit4.h(qreg4[0])\n",
    "mycircuit4.h(qreg4[0])\n",
    "\n",
    "# measure the first qubit, and store the result in the first classical bit\n",
    "mycircuit4.measure(qreg4,creg4)\n",
    "\n",
    "print(\"Everyhing looks fine, let's continue ...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# draw the circuit\n",
    "drawer(mycircuit4)\n",
    "# reexecute me if you DO NOT see the circuit diagram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# execute the circuit and read the results\n",
    "job = execute(mycircuit4,Aer.get_backend('qasm_simulator'),shots=10000)\n",
    "\n",
    "counts4 = job.result().get_counts(mycircuit4)\n",
    "print(counts4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
